// packageA/pages/iotWarehousePerson/iotWarehousePerson.js
const app = getApp();
import CommonService from '../../../utils/service/commonService.js';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
const util = require('../../../utils/util');
import Base64 from '../../../utils/Base64.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //传参
    paramObj:{},
    //领用设备列表
    list:[],
    //页码
    page: 1,
    //是否无更多
    notMore: true,
    //无数据
    showNothing:false,
    //搜索关键词
    searchValue:'',
    //查询类型下拉
    optionType: [{
      text: '领用模块',
      value: 1
    },{
      text: '更换模块',
       value: 2
    }],
    valueType: 1,
    //状态下拉
    optionState: [{
      text: '全部状态',
      value: -1
    },{
      text: '未安装',
      value: 0
    },{
      text: '已安装',
      value: 1
    },{
      text: '模块被本人重新替换',
      value: 2
    },{
      text: '模块被他人重新替换',
      value: 3
    }],
    valueState: 0,
    optionState2: [{
      text: '全部状态',
      value: -1
    },{
      text: '更换模块收入旧模块',
      value: 4
    },{
      text: '发回审批中',
      value: 5
    },{
      text: '已审批通过',
      value: 6
    },{
      text: '已审批驳回',
      value: 7
    },{
      text: '已转回仓库',
      value: 8
    }],
    valueState2: -1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //查询领用的设备
    this.getList();
  },

  //查询领用的设备
  getList(){
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '加载中...'
    });

    let param = {
      isAuth: true,
      className: 'imeiWarehouseInFactoryM',
      data: {
        collectUserId: app.globalData.userInfo.id,
        selectFields: ["id","imeiWS","collectUser","collectUserId"],
        sort: {
          id: -1
        },
        page: {
          page: this.data.page,
          pageSize: 10
        }
      }
    }

    //模糊查询
    if(this.data.searchValue){
      param.data['imeiWS.imei']='%' +this.data.searchValue+ '%';
    }

    CommonService.goFindList(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result;
        //将imeiWS放入一个数组
        let arr=[];
        result.forEach(item => {
          if(item.imeiWS && item.imeiWS.length){
            arr=arr.concat(item.imeiWS);
          }
        });

        //状态筛选
        let stateArr=[];
        arr.forEach(item => {
          //状态名称
          switch(item.state){
            case 0:
              item.self_state='未安装';
              item.self_stateClass='c-7';
              break;
            case 1:
              item.self_state='已安装';
              item.self_stateClass='c-1';
              break;
            case 2:
              item.self_state='模块被本人重新替换';
              item.self_stateClass='c-8';
              break;
            case 3:
              item.self_state='模块被他人重新替换';
              item.self_stateClass='c-2';
              break;
            case 4:
              item.self_state='更换模块收入旧模块';
              item.self_stateClass='c-6';
              break;
            case 5:
              item.self_state='发回审批中';
              item.self_stateClass='c-4';
              break;
            case 6:
              item.self_state='已审批通过';
              item.self_stateClass='c-5';
              break;
            case 7:
              item.self_state='已审批驳回';
              item.self_stateClass='c-9';
              break;
            case 8:
              item.self_state='已转回仓库';
              item.self_stateClass='c-3';
              break;
          }

          //时间
          if (item.createDate){
            item.self_createDate=util.timeFormat(item.createDate);
          }

          //状态筛选
          if(this.data.valueState==-1){
            stateArr.push(item);
          }else{
            if(item.state==this.data.valueState){
              stateArr.push(item);
            }
          }
        });

        console.log('领用模块');
        console.log(stateArr);

        if(stateArr.length){
          this.setData({
            list: this.data.list.concat(stateArr)
          })
          Toast.clear();
        }else{
          this.setData({
            showNothing: true
          })
          Toast.clear();
        }

      }else{
        this.setData({
          notMore: false
        })
        if(this.data.page==1){
          this.setData({
            showNothing: true
          })
          Toast.clear();
        }else{
          Toast.fail('没有更多了');
        }
      }
    }).catch((error) => {
      this.setData({
        showNothing: true
      })
      Toast.clear();
    })
  },

  //查询更换的设备
  getReplaceList(){
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '加载中...'
    });

    let param = {
      isAuth: true,
      className: 'imeiWarehouseInFactoryS',
      data: {
        userId: app.globalData.userInfo.id,
        selectFields: ["id","imei","state","num","createDate"],
        sort: {
          id: -1
        },
        page: {
          page: this.data.page,
          pageSize: 10
        }
      }
    }

    //模糊查询
    if(this.data.searchValue){
      param.data.imei='%' +this.data.searchValue+ '%';
    }

    //状态筛选
    if(this.data.valueState2!=-1){
      param.data.state=this.data.valueState2;
    }

    CommonService.goFindList(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result;

        result.forEach(item => {
          //状态名称
          switch(item.state){
            case 0:
              item.self_state='未安装';
              item.self_stateClass='c-7';
              break;
            case 1:
              item.self_state='已安装';
              item.self_stateClass='c-1';
              break;
            case 2:
              item.self_state='模块被本人重新替换';
              item.self_stateClass='c-8';
              break;
            case 3:
              item.self_state='模块被他人重新替换';
              item.self_stateClass='c-2';
              break;
            case 4:
              item.self_state='更换模块收入旧模块';
              item.self_stateClass='c-6';
              break;
            case 5:
              item.self_state='发回审批中';
              item.self_stateClass='c-4';
              break;
            case 6:
              item.self_state='已审批通过';
              item.self_stateClass='c-5';
              break;
            case 7:
              item.self_state='已审批驳回';
              item.self_stateClass='c-9';
              break;
            case 8:
              item.self_state='已转回仓库';
              item.self_stateClass='c-3';
              break;
          }

          //时间
          if (item.createDate){
            item.self_createDate=util.timeFormat(item.createDate);
          }
        });

        console.log('更换模块');
        console.log(result);

        this.setData({
          list: this.data.list.concat(result)
        })
        Toast.clear();
      }else{
        this.setData({
          notMore: false
        })
        if(this.data.page==1){
          this.setData({
            showNothing: true
          })
          Toast.clear();
        }else{
          Toast.fail('没有更多了');
        }
      }
    }).catch((error) => {
      this.setData({
        showNothing: true
      })
      Toast.clear();
    })
  },

  //切换查询类型下拉菜单
  onChangeType({detail}){
    this.setData({
      valueType: detail
    })
    if(detail==1){
      this.refreshListAndPage();
    }else if(detail==2){
      this.refreshListAndPage2();
    }
  },

  //切换状态下拉菜单-领用设备
  onChangeState({detail}){
    this.setData({
      valueState: detail
    })
    this.refreshListAndPage();
  },

  //切换状态下拉菜单-更换设备
  onChangeState2({detail}){
    this.setData({
      valueState2: detail
    })
    this.refreshListAndPage2();
  },

  //刷新列表和页码
  refreshListAndPage(){
    this.setData({
      list: [],
      page:1,
      notMore: true,
      showNothing: false
    })
    this.getList();
  },

  //刷新列表和页码
  refreshListAndPage2(){
    this.setData({
      list: [],
      page:1,
      notMore: true,
      showNothing: false
    })
    this.getReplaceList();
  },

  //获取搜索词
  onChangeSearch(e) {
    this.setData({
      searchValue: e.detail.value
    })
  },

  //点击搜索
  onSearch(){
    if(this.data.valueType==1){
      this.refreshListAndPage();
    }else if(this.data.valueType==2){
      this.refreshListAndPage2();
    }
  },

  //查看更换信息
  goToReplace({currentTarget}){
    let detail=currentTarget.dataset.detail;
    let obj={
      id: detail.id
    }
    wx.navigateTo({
      url: "/packageA/pages/iotWarehouseReplace/iotWarehousePersonReplace?paramObj="+JSON.stringify(obj)
    })
  },

  //返回维护
  goToBack({currentTarget}){
    let detail=currentTarget.dataset.detail;
    let obj={
      id: detail.id,
      imei: detail.imei
    }
    wx.navigateTo({
      url: "/packageA/pages/iotWarehouseBack/iotWarehouseBack?paramObj="+JSON.stringify(obj)
    })
  },

  //尝试使用
  onUse({currentTarget}){
    let detail=currentTarget.dataset.detail;
    let param={
      className:'imeiWarehouseInFactoryS',
      data:{
        id: detail.id,
        state: 0
      }
    }

    CommonService.goSave(param).then(res=>{
      if (res.statusCode == 200 && res.data.success == true){
        if(this.data.valueType==1){
          this.refreshListAndPage();
        }else if(this.data.valueType==2){
          this.refreshListAndPage2();
        }
      }else{
        Toast.fail('操作失败');
      }
    }).catch((error) => {
      Toast.fail('操作失败');
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.notMore){
      this.setData({
        page: this.data.page + 1
      })

      if(this.data.valueType==1){
        this.getList();
      }else if(this.data.valueType==2){
        this.getReplaceList();
      }
    }
  }
})
